package com.imu.purse.modules.app.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.imu.constant.Constant;
import com.imu.core.base.BaseService;
import com.imu.purse.modules.app.dao.UserCheckMapper;
import com.imu.purse.modules.app.entity.UserCheckEntity;
import com.imu.purse.modules.app.service.UserCheckService;
import com.xiaoleilu.hutool.util.ArrayUtil;
import com.xiaoleilu.hutool.util.NumberUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
public class UserCheckServiceImpl extends BaseService<UserCheckEntity> implements UserCheckService 
{
	@Override
    @Transactional
    public int save(final UserCheckEntity userCheck)
	{
		this.deleteByUserId(userCheck.getUserId());
		userCheck.setTimeCreate(new Date());
    	//userCheck.setTimeUpdate(new Date());
		super.insert(userCheck);
		redisTemplate.opsForHash().put(Constant.USER_CHECK_VAL, userCheck.getUserId().toString(), JSON.toJSONString(userCheck));
    	return 1;
	}
	
	@Override
	@Transactional
    public int update(final UserCheckEntity userCheck) 
    {
    	//userCheck.setTimeUpdate(new Date());
    	return mapper.updateByPrimaryKeySelective(userCheck);
    }
    
    @Override
	@Transactional
    public int saveUpdate(final UserCheckEntity userCheck)
    {
    	int len = 0;
    	
    	if(1 > (len = this.update(userCheck)))
    	{
        	len = this.insert(userCheck);
        	//
        	redisTemplate.opsForHash().put(Constant.USER_CHECK_VAL, userCheck.getUserId().toString(), JSON.toJSONString(userCheck));
    	}
    	
    	return len;
    }
    
 	@Override
	@Transactional
    public boolean delete(final String ...ids)
    {
    	if(ArrayUtil.isEmpty(ids)) return false;
    	
    	for (String id : ids) 
    	{
    		if(StrUtil.isBlank(id)) continue;
    		
    		if(NumberUtil.isNumber(id))
    		{
        		mapper.deleteByPrimaryKey(Long.valueOf(id));
    		}
		}
    	
    	return true;
    }
 	
 	@Override
	@Transactional
    public boolean deleteByUserId(Long userId)
    {
 		mapper.deleteByUserId(userId);
 		redisTemplate.opsForHash().delete(Constant.USER_CHECK_VAL, userId.toString());
    	return true;
    }
    
    @Override
    public UserCheckEntity findById(final Long id)
    {
    	if(null == id) return null;
    	return mapper.selectByPrimaryKey(id);
    }
    
    @Override
    public List<UserCheckEntity> queryList(final UserCheckEntity userCheck)
    {
    	if (userCheck.getPage() != null && userCheck.getLimit() != null)
    	{
            PageHelper.startPage(userCheck.getPage(), userCheck.getLimit());
        }
    	
    	return mapper.findByPage(userCheck);
    }
    
    @Override
	public UserCheckEntity findByUserId(Long userId) {
    	UserCheckEntity entity = new UserCheckEntity();
		Object obj = redisTemplate.opsForHash().get(Constant.USER_CHECK_VAL, userId.toString());
		if(null == obj){
			entity.setUserId(userId);
			List<UserCheckEntity> list = mapper.select(entity);
			if(!CollectionUtils.isEmpty(list)){
				entity = list.get(0);
				redisTemplate.opsForHash().put(Constant.USER_CHECK_VAL, entity.getUserId().toString(), JSON.toJSONString(entity));
			}
		}else {
			entity = JSON.parseObject(obj.toString(), UserCheckEntity.class);
		}
		return entity;
	}
	@Autowired
	private UserCheckMapper mapper;
	
	@Autowired
    private RedisTemplate<String, String> redisTemplate;

	
}
